//
//  WPRegisterView.m
//  wobingwoyi
//
//  Created by WGP on 16/8/1.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPRegisterView.h"
#import "WLCaptcheButton.h"
#import "NSString+WPSizeOfString.h"

@interface WPRegisterView ()

@property(nonatomic,assign)BOOL isSending;

@end
@implementation WPRegisterView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self registerInfoView];
    }
    return self;
}

-(void)registerInfoView
{
    self.backgroundColor = [UIColor whiteColor];
    
    UIView *firstLine = [self lineView];
    UIView *secondLine = [self lineView];
    UIView *thirdLine = [self lineView];
    UIView *fourthLine = [self lineView];
    
    [self addSubview:firstLine];
    [self addSubview:secondLine];
    [self addSubview:thirdLine];
    [self addSubview:fourthLine];
    
    self.phoneNumField = [UITextField new];
    //    self.phoneNumField.backgroundColor = [UIColor orangeColor];
    self.verifyField = [UITextField new];
    self.passwordField = [UITextField new];
    
    self.phoneNumField.placeholder = @"请输入手机号码";
    [self.phoneNumField becomeFirstResponder];
    self.phoneNumField.keyboardType = UIKeyboardTypeNumberPad;
    self.verifyField.placeholder = @"验证码";
    self.verifyField.keyboardType = UIKeyboardTypeNumberPad;
    self.passwordField.placeholder = @"设置不低于6位的密码";
    
    
    self.phoneNumField.font = [UIFont systemFontOfSize:14];
    self.verifyField.font = [UIFont systemFontOfSize:14];
    self.passwordField.font = [UIFont systemFontOfSize:14];
    self.passwordField.secureTextEntry = YES;
    
    [self addSubview:self.phoneNumField];
    [self addSubview:self.verifyField];
    [self addSubview:self.passwordField];
    
    WLCaptcheButton *getVerificationCodeBtn = [WLCaptcheButton new];
    getVerificationCodeBtn.identifyKey = @"timerKey";
//    getVerificationCodeBtn.backgroundColor = [UIColor orangeColor];
    [getVerificationCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    getVerificationCodeBtn.disabledTitleColor = [UIColor lightGrayColor];
    [getVerificationCodeBtn setTitleColor:GlobalColor forState:UIControlStateNormal];
    getVerificationCodeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
//    [getVerificationCodeBtn sizeToFit];
    [getVerificationCodeBtn addTarget:self action:@selector(getVerificationBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:getVerificationCodeBtn];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"line"]];
    [self addSubview:imageView];
    
    UIButton *secureBtn = [WPBtnNoHighLight buttonWithType:UIButtonTypeCustom];
    [secureBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-biyan"] forState:UIControlStateNormal];
    [secureBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-yanjing"] forState:UIControlStateSelected];
    [secureBtn addTarget:self action:@selector(secureBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:secureBtn];
    
    [firstLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    
    [secondLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15*KWIDTH_SCALE);
        make.height.mas_equalTo(0.5);
        make.right.equalTo(self);
        make.top.equalTo(self).offset(44*SCALE);
    }];
    
    [thirdLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15*KWIDTH_SCALE);
        make.height.mas_equalTo(0.5);
        make.right.equalTo(self);
        make.top.equalTo(secondLine.mas_bottom).offset(44*SCALE);
    }];
    
    [fourthLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.phoneNumField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(secondLine);
        make.top.equalTo(self);
        make.height.mas_equalTo(44*SCALE);
        make.right.equalTo(self.mas_centerX);
    }];
    
    [self.verifyField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.phoneNumField);
        make.top.equalTo(self.phoneNumField.mas_bottom);
    }];
    
    [self.passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.phoneNumField);
        make.top.equalTo(self.verifyField.mas_bottom);
    }];
    
    
    [getVerificationCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.verifyField);
        make.right.equalTo(self).offset(-10*KWIDTH_SCALE);
    }];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(getVerificationCodeBtn);
        make.left.equalTo(getVerificationCodeBtn).offset(-10*KWIDTH_SCALE);
    }];
    
    [secureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.passwordField);
        make.right.equalTo(self).offset(-10*KWIDTH_SCALE);
    }];
}

-(void)getVerificationBtnDidClick:(WLCaptcheButton *)btn
{
    if (![NSString isMobile:self.phoneNumField.text])
    {
        [self showErrorInfoWithStatus:@"手机号码格式不正确"];
        return ;
    }
    
//    http://192.168.1.105:8080//wbwy/user/sendMessage.do?telephone=18225529869
    
    if (self.isSending)
    {
        return ;
    }
    
    self.isSending = YES;
    
    NSString *url = @"user/sendMessage.do";
    NSDictionary *parameters = @{@"telephone":self.phoneNumField.text};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self showSuccessInfoWithStatus:@"验证码已发送"];
        [btn fire];
        
        self.isSending = NO;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error)
        {
             [self showErrorInfoWithStatus:@"网络错误"];
        }
        
        self.isSending = NO;
        
    }];
    

    
    
}

-(void)secureBtnDidClick:(UIButton *)secureBtn
{
    secureBtn.selected = !secureBtn.selected;
    self.passwordField.secureTextEntry = !self.passwordField.secureTextEntry;
}

-(UIView *)lineView
{
    UIView *lineView = [UIView new];
    lineView.backgroundColor  = GLobalLineColor;
    return lineView;
}



@end
